package src;

import javax.swing.plaf.ViewportUI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class OrderingSystem {
    static List<Dish> dishes = new ArrayList<>();
    static List<Order> orders = new ArrayList<>();
    static Scanner scanner = new Scanner(System.in);
    static int i=1;
    public static void main(String[] args) {
        dishes.add(new Dish(1,"鱼香肉丝",18.00,9));
        dishes.add(new Dish(2,"糖醋里脊",21.00,5));
        dishes.add(new Dish(3,"麻婆豆腐",12.00,2));
        while(true){
            System.out.println("欢迎使用”漂亮团外卖“");
            System.out.println("**********************");
            System.out.println("1、我要订餐");
            System.out.println("2、我的订单");
            System.out.println("3、签收订单");
            System.out.println("4、删除订单");
            System.out.println("5、我要点赞");
            System.out.println("6、退出系统");
            System.out.println("**********************");
            System.out.print("请选择:");
            int temp = scanner.nextInt();
            switch (temp){
                case 1:
                    addOrder();
                    break;
                case 2:
                    showMyOrder();
                    break;
                case 3:
                    qianShou();
                    break;
                case 4:
                    delete();
                    break;
                case 5:
                    userLikeCount();
                    break;
                case 6:
                    System.exit(0);
                    break;
            }
        }

    }

    static void addOrder(){
        Order order = new Order();
        System.out.println("输入订餐人姓名");
        String name = scanner.next();
        double tempSumPrice;
        order.OrderName = name;

        order.id = i++;
        while (true){
            tempSumPrice=0;
            System.out.println("菜品序号:\t\t菜品名称:\t\t菜品价格:\t\t点赞数:");
            for (Dish dish:
                 dishes) {
                System.out.println("\t"+dish.dishId+"\t\t"+ dish.dishName+"\t\t"+dish.dishPrice+"\t\t"+dish.likeCount);
            }
            System.out.println("请输入菜品序号以及份数");
            int tempId = scanner.nextInt();
            int tempCount = scanner.nextInt();

            double tempPrice = dishes.get(tempId-1).dishPrice;
            String tempName  = dishes.get(tempId-1).dishName;

            SelectDish selectDish = new SelectDish(tempId, tempName, tempPrice, tempCount);

            order.selectDishes.add(selectDish);

            tempSumPrice+=tempPrice*tempCount;
            System.out.println("1、不点了");
            System.out.println("2、继续点");
            System.out.println("请输入:");
            int n = scanner.nextInt();
            if (n == 1) {
                break;
            }
        }
        System.out.println("请输入送餐时间:");
        int time = scanner.nextInt();
        order.outTime = time;
        System.out.println("请输入送餐地址:");
        order.address = scanner.next();
        order.stats = 0;
        if(tempSumPrice<50){
            order.sumPrices = tempSumPrice+6;
        }else {
            order.sumPrices = tempSumPrice;
        }
        orders.add(order);
    }
    static void showMyOrder(){
        System.out.println("序号\t订餐人姓名\t订餐信息(名字和份数)\t\t\t\t送餐时间\t送餐地址\t\t状态\t\t总金额");
        for (Order order:
                orders) {
            System.out.print(order.id+"\t"+order.OrderName+"\t\t\t[");
            for (SelectDish selectDish:
                 order.selectDishes) {

                System.out.print(selectDish.selectDishName+"\t"+selectDish.selectDishCount+"份"+" ");

            }
            System.out.print("]");
            String stats;
            if(order.stats == 0){
                 stats = "已预定";
            }else{
                 stats = "已签收";
            }
            System.out.println("\t\t\t\t\t"+order.outTime+"\t\t"+order.address+"\t\t\t"+stats+"\t"+order.sumPrices+"元");
        }

    }

    static void qianShou(){
        System.out.println("请输入要签收的订单号:");
        int i = scanner.nextInt();

        for (int j = 0; j < orders.size(); j++) {
            if(orders.get(j).id == i){
                orders.get(j).stats = 1;
                System.out.println("修改成功");
                return;
            }
        }
        System.out.println("修改失败");

    }
    static void delete(){
        System.out.println("请输入要删除的订单号");
        int i = scanner.nextInt();

        for (int j = 0; j < orders.size(); j++) {
            if(orders.get(j).id == i){
                if (orders.get(j).stats == 1){
                    orders.remove(orders.get(j));
                    System.out.println("删除成功");
                }else {
                    System.out.println("该订单还未签收");
                }
            }
        }
    }

    static void userLikeCount(){
        showDish();
        System.out.println("请输入要点赞的菜品号");
        int i = scanner.nextInt();
        dishes.get(i - 1).likeCount++;
        System.out.println("点赞成功");
    }

    static void showDish(){
        System.out.println("菜品序号:\t\t菜品名称:\t\t菜品价格:\t\t点赞数:");
        for (Dish dish:
                dishes) {
            System.out.println("\t"+dish.dishId+"\t\t"+ dish.dishName+"\t\t"+dish.dishPrice+"\t\t"+dish.likeCount);
        }
    }
}
